package backup_old_classes;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;


public class GraphicCreator_OLD {

    private String path;
    private SimpleGraphicNodeDB db;
    private  List<SimpleGraphicNode> graphicNodes;

    /**
     * Parse the document in input (xml file) and maps the xml element into 
     * a SimpleGraphicNode object that can be generated by generateGraphic()
     * 
     * @param filename a name of xml file to parse to create graphics 
     * @throws ParserConfigurationException 
     */
    public GraphicCreator_OLD(String filename) throws ParserConfigurationException
    {   
        path = filename;
        graphicNodes = null;

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document document = null;
        try 
        {
            document = builder.parse(new FileInputStream(path));
            document.getDocumentElement().normalize();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //call the SimpleGraphicNodeDB constructor to can receive a list of 
        //SimpleGraficNode object from the DOM just parsed
        db = new SimpleGraphicNodeDB(document);
        graphicNodes = db.getAllNodes();
    }


    /**
     * Generate the Swing graphic from the list of SimpleGraphicNode object
     * parsed in the constructor. It creates a new JFrame and for each 
     * SimpleGraphicNode in the list call @method printElement.
     */
    public void generateGraphic()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Aicas/AndroidApp");
        
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER,5,5);
        flowLayout.setHgap(1000);//in this way each component added to the layout will be added in a new line
        frame.getContentPane().setLayout(flowLayout);
       
        
        frame.setVisible(true);


        for(int i=0; i<graphicNodes.size(); i++)
        {
            SimpleGraphicNode graphicElement = graphicNodes.get(i);
            printElement(frame, graphicElement);
        }
        frame.pack();
        frame.setSize(300, 500);

    }


    /**
     * Takes a SimpleGraphicNode and creates a corrispondent Swing object 
     * and prints it in a frame
     * @param frame  the JFrame in wich print the Swing object
     * @param graphicElement the SimpleGrapchicNode element to convert in Swing object
     */
    public void printElement(JFrame frame, SimpleGraphicNode graphicElement) 
    {   
        if(graphicElement.getGraphicElement().equals("TextView"))
        {
            JLabel label = new JLabel();
            label.setText(graphicElement.getText());
            if(label.getText().equals(""))
            {
                label.setText("Blank label here");
            }
            frame.getContentPane().add(label);
        }
        else if(graphicElement.getGraphicElement().equals("Button"))
        {
            JButton button = new JButton();
            button.setText(graphicElement.getText());
            frame.getContentPane().add(button);
        }
        else if(graphicElement.getGraphicElement().equals("EditText"))
        {
            JTextField text = new JTextField(20);
            text.setEditable(true);
            frame.getContentPane().add(text);
        }
         
    }

    /**
     * Runs the thread that permit to show the graphic components
     */
    public void showGraphic()
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                generateGraphic();
            }
        });
    }


}























